// pages/student/rechargeable/rechargemoney.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');
var QRCode = require('../../../utils/qrcode.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        price:'',
        pricefalse:false,
        phone:'',
        qrcode:'',
        cordefalse:false,
    },
    changeprice(e){
        var that = this;
        var price = e.detail.value;
        if (Number(String(price).indexOf(".") + 1) > 0) {
            that.setData({
                price: Math.floor(price)
            })
            wx.showToast({
                title: '充值金额不能为小数',
                icon: 'none'
            })
        }
    },
    clisecorde(){
        wx.navigateBack();
        // this.setData({
        //     cordefalse: false,
        //     pricefalse:false,
        //     qrcode:'',
        // })
    },
    formSubmit(e){
        var that = this;
        var price = e.detail.value.price;
        that.setData({
            price: price
        })
        if (Number(String(price).indexOf(".") + 1) > 0) {
            wx.showToast({
                title: '充值金额不能为小数',
                icon: 'none'
            })
        } else if (Number(price)<=0){
            wx.showToast({
                title: '充值金额至少为1',
                icon: 'none'
            })
        }else{
            that.setData({
                pricefalse:true
            })
            wx.showLoading({
                title: '加载中~',
            })
            share.wxRequest('crmapi/Student/bugCardMoney', { phone: that.data.phone, amount: price }, function (res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    // console.log(data);
                    var url = CONFIG.API_corde + '?phone=' + that.data.phone + '&is_skillfrom=1&price=' + price + '&ucp_id=' + data.log_id + '&appid=' + data.appid+'';
                    console.log(url);
                    
                    let qrcode = new QRCode('myQrcode', {
                        text: url,
                        width: 200,
                        height: 200,
                        // padding: 10, // 生成二维码四周自动留边宽度，不传入默认为0
                        // colorDark: "#1CA4FC",
                        // colorLight: "white",
                        correctLevel: QRCode.CorrectLevel.H, // 二维码可辨识度
                        callback: (res) => {
                        }
                    })
                    that.setData({
                        qrcode: qrcode,
                        cordefalse:true
                    })
                }else{
                    that.setData({
                        pricefalse: false
                    })
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
                wx.hideLoading();
            })
        }
    },
    savecorder(){
        var that = this;
        var qrcode = that.data.qrcode;
        qrcode.exportImage(function (path) {
            // console.log(path);
            share.sendImage(path);
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var phone = options.phone;
        // var phone = '18335182454';
        
            
        that.setData({
            phone:phone,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})